<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="toggle" :class="{ active: isShow }">
      <span class="placeholder" v-if="!fullLocation">请选择配送地址</span>
      <span class="value" v-else>
        {{ fullLocation }}
      </span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show="isShow">
      <div class="loading" v-if="!currList"></div>
      <template v-else>
        <span class="ellipsis" v-for="item in currList" :key="item.code">{{
          item.name
        }}</span>
      </template>
    </div>
  </div>
</template>

<script>
import { computed, ref } from 'vue'
import { onClickOutside } from '@vueuse/core'
import axios from 'axios'
// 获取城市数据
const getCityData = () => {
  return new Promise((resolve) => {
    if (window.cityData) {
      resolve(window.cityData)
    } else {
      const url =
        'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
      // axios是异步操作，当封装在一个函数中时，返回的值无法得到及时更新，且无法传出使用
      // 因此，需要用到 new Promise 的 resolve 参数来传出axios中返回的值
      axios.get(url).then(({ data }) => {
        // console.log(data)
        window.cityData = data
        resolve(window.cityData)
      })
    }
  })
}
export default {
  props: {
    fullLocation: {
      type: String,
      default: ''
    }
  },
  name: 'XtxCity',
  setup () {
    const isShow = ref(false)
    // const isLoading = ref(false)
    const cityData = ref([])

    const show = async () => {
      isShow.value = true
      // isLoading.value = true
      cityData.value = await getCityData()
      // console.log('123')
      // isLoading.value = false
    }
    const hide = () => {
      isShow.value = false
    }
    // 切换展开收起
    const toggle = () => {
      isShow.value ? hide() : show()
    }
    // 点击其他位置隐藏
    const target = ref(null)
    onClickOutside(target, () => {
      hide()
    })

    const currList = computed(() => {
      const currList = cityData.value
      // console.log(currList)
      return currList
    })
    // console.log(currList)

    return { isShow, toggle, target, currList }
  }
}
</script>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    .loading {
      height: 290px;
      width: 100%;
      background: url(~@/assets/images/loading.gif) no-repeat center;
    }
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
